﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CashDriver;
using CashResource;
using CashCommon;
using System.Drawing.Printing;
using System.Drawing;
using System.Globalization;
using RSDN;

namespace Cash.Model
{
    public interface IPrintDoc
    {
        void Init();
        /// <summary>
        /// Печать чека
        /// </summary>
        /// <param name="cashRegister">Объект класса чек</param>
        void PrintCash(CashRegister cashRegister);
        /// <summary>
        ///  Печать копии чека
        /// </summary>
        /// <param name="cashRegister">Объект класса чек</param>
        void PrintCopyCash(CashRegister cashRegister);
        /// <summary>
        /// Предварительный просмотр печати чека
        /// </summary>
        /// <param name="cashRegister">Объект класса чек</param>
        void PrinPreview(CashRegister cashRegister);
        void ReportWithoutCleaning();
        void ReportWithCleaning();
    }

    public class PrintDoc01 : IPrintDoc
    {
        public void Init()
        {
            _formMain = Tools.MainForm as FormMain;
        }

        public void PrintCash(CashRegister cashRegister)
        {
            try
            {

                KKM kkm = KKM.Inst;
                FrkDrv.Inst.Password = 30;
                FrkDrv.Inst.CheckType = 0;

                PayNDS payNDS = Tools.GetTax(cashRegister.Amount, cashRegister.N_Tax);

                if (cashRegister.Amount > 0)
                    payNDS = Sale(cashRegister, kkm, payNDS);
                else
                    ReturnSale(cashRegister, kkm);
            }
            catch (Exception err)
            {
                throw new ECash(MessageType.AllertLog, DriverError.CheckPrintError, err);
            }
            finally
            {
                FrkDrv.Inst.CloseCheck();
                cashRegister.Number = FrkDrv.Inst.OpenDocumentNumber+Properties.Settings.Default.CheckNumberShift; 
            }

        }
        private static PayNDS Sale(CashRegister cashRegister, KKM kkm, PayNDS payNDS)
        {
            FrkDrv.Inst.OpenCheck();

            FrkDrv.Inst.Price = Convert.ToDecimal(payNDS.Value, CultureInfo.InvariantCulture);
            FrkDrv.Inst.Quantity = 1;
            FrkDrv.Inst.Department = 1;
            FrkDrv.Inst.StringForPrinting = cashRegister.Sale_Items_Name;
            FrkDrv.Inst.Connect();
            FrkDrv.Inst.Sale();

            FrkDrv.Inst.Price = Convert.ToDecimal(payNDS.Tax, CultureInfo.InvariantCulture);
            FrkDrv.Inst.Quantity = 1;
            FrkDrv.Inst.Department = 1;
            FrkDrv.Inst.StringForPrinting = FieldsName.CashAccountTax;
            FrkDrv.Inst.Connect();
            FrkDrv.Inst.Sale();

            // Внесенная сумма
            FrkDrv.Inst.Summ1 = Convert.ToDecimal(cashRegister.Amount, CultureInfo.InvariantCulture);

            FrkDrv.Inst.UseReceiptRibbon = true;
            FrkDrv.Inst.UseJournalRibbon = false;

            CashPrintText("-----------------------------------");

            List<string> ls = new List<string>();
            KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Принято от:", cashRegister.AbonentName), kkm.WidthCheck);
            for (int i = 0; i < ls.Count; i++)
                CashPrintText(ls[i]);

            ls.Clear();
            KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Основание: ", cashRegister.C_Foundation), kkm.WidthCheck);
            for (int i = 0; i < ls.Count; i++)
                CashPrintText(ls[i]);
            
            CashPrintText("-----------------------------------");
            
            return payNDS;
        }
        private static void ReturnSale(CashRegister cashRegister, KKM kkm)
        {
            FrkDrv.Inst.Price = Convert.ToDecimal(-cashRegister.Amount, CultureInfo.InvariantCulture);
            FrkDrv.Inst.Summ1 = Convert.ToDecimal(-cashRegister.Amount, CultureInfo.InvariantCulture);
            FrkDrv.Inst.Quantity = 1;
            FrkDrv.Inst.Department = 1;
            FrkDrv.Inst.StringForPrinting = cashRegister.Sale_Items_Name;
            FrkDrv.Inst.Connect();
            FrkDrv.Inst.ReturnSale();
            CashPrintText("--------------------");

        }
        private static void CashPrintText(string str)
        {
            FrkDrv.Inst.StringForPrinting = str;
            FrkDrv.Inst.PrintString();
            FrkDrv.Inst.StringForPrinting = "";
        }
        public void PrintCopyCash(CashRegister cashRegister)
        {
            _cashRegister = cashRegister;
            _formMain.Print(new PrintPageEventHandler(DrawCashCopy));
        }
        public void PrinPreview(CashRegister cashRegister)
        {
            _formMain = Tools.MainForm as FormMain;
            _cashRegister = cashRegister;
            if (_formMain != null)
            {
                _formMain.PreviewDialogShow(new PrintPageEventHandler(DrawCashCopy));
            }
        }
        public void ReportWithoutCleaning()
        {
            FrkDrv.Inst.PrintReportWithoutCleaning();
        }
        public void ReportWithCleaning()
        {
            FrkDrv.Inst.PrintReportWithCleaning();
        }

        #region Печать копии чека

        private void DrawCashCopy(object sender, PrintPageEventArgs e)
        {
            Pen linelPen1 = new Pen(Color.Black, 1);
            Pen linelPen2 = new Pen(Color.Black, 2);
            KKM kkm = KKM.Inst;
            try 
	        {	        
                float topMargin = e.MarginBounds.Top + 60;
                float leftMargin = e.MarginBounds.Left;
                
                float heigthText1 = fontText1.GetHeight(e.Graphics) + 3;
                float heigthText3 = fontText3.GetHeight(e.Graphics) + 3;

                // Строка 1
                e.Graphics.DrawString(CashEmployer.Inst.SvDeptName, fontText1B, Brushes.Black, leftMargin, topMargin, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin, topMargin + heigthText1, leftMargin + 280, topMargin + heigthText1);

                #region Номер документа, дата составления

                e.Graphics.DrawString("КОПИЯ ЧЕКА", fontText1B, Brushes.Black, leftMargin + 80, topMargin + 60, new StringFormat());
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 140, 50);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 140, 50);
                e.Graphics.DrawRectangle(linelPen2, leftMargin + 280, topMargin + 67, 140, 23);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 70, 50);
                e.Graphics.DrawString("Номер", fontText3, Brushes.Black, leftMargin + 300, topMargin + 42, new StringFormat());
                e.Graphics.DrawString("документа", fontText3, Brushes.Black, leftMargin + 292, topMargin + 51, new StringFormat());
                e.Graphics.DrawString("Дата", fontText3, Brushes.Black, leftMargin + 370, topMargin + 42, new StringFormat());
                e.Graphics.DrawString("составления", fontText3, Brushes.Black, leftMargin + 355, topMargin + 51, new StringFormat());
                e.Graphics.DrawString(_cashRegister.Number.ToString(), fontText2, Brushes.Black, leftMargin + 285, topMargin + 70, new StringFormat());
                e.Graphics.DrawString(_cashRegister.DatePrint.ToShortDateString(), fontText2, Brushes.Black, leftMargin + 350, topMargin + 70, new StringFormat());

                #endregion

                #region Таблица

                e.Graphics.DrawRectangle(linelPen2, leftMargin, topMargin + 100, 420, 100);
                e.Graphics.DrawRectangle(linelPen2, leftMargin, topMargin + 180, 420, 20);

                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 60, 100);
                e.Graphics.DrawString("Дебет", fontText2, Brushes.Black, leftMargin+13, topMargin+134, new StringFormat());
                e.Graphics.DrawString("Кредит", fontText2, Brushes.Black, leftMargin + 145, topMargin + 103, new StringFormat());

                e.Graphics.DrawString("код струк-", fontText3, Brushes.Black, leftMargin + 83, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("турного под-", fontText3, Brushes.Black, leftMargin + 77, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("разделения", fontText3, Brushes.Black, leftMargin + 79, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("кореспон-", fontText3, Brushes.Black, leftMargin + 145, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("дирующий", fontText3, Brushes.Black, leftMargin + 142, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("счет, субсч.", fontText3, Brushes.Black, leftMargin + 140, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("код аналити-", fontText3, Brushes.Black, leftMargin + 197, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("ческого", fontText3, Brushes.Black, leftMargin + 206, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("учета", fontText3, Brushes.Black, leftMargin + 210, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("Сумма,", fontText2, Brushes.Black, leftMargin + 268, topMargin + 120, new StringFormat());
                e.Graphics.DrawString("руб. коп.", fontText2, Brushes.Black, leftMargin + 266, topMargin + 132, new StringFormat());
                
                e.Graphics.DrawString("Код", fontText2, Brushes.Black, leftMargin + 348, topMargin + 120, new StringFormat());
                e.Graphics.DrawString("целевого", fontText2, Brushes.Black, leftMargin + 336, topMargin + 130, new StringFormat());
                e.Graphics.DrawString("назначения", fontText2, Brushes.Black, leftMargin + 330, topMargin + 140, new StringFormat());

                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 75, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin+75, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin+135, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 195, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 255, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 325, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 400, 100);

                e.Graphics.DrawString("50.1", fontText2, Brushes.Black, leftMargin + 3, topMargin + 182, new StringFormat());
                e.Graphics.DrawString("62.7", fontText2, Brushes.Black, leftMargin + 137, topMargin + 182, new StringFormat());
                e.Graphics.DrawString(_cashRegister.Amount.ToString("C"), fontText2, Brushes.Black, leftMargin + 257, topMargin + 182, new StringFormat());

                #endregion

                #region Текст

                float topMarginNext = topMargin+220;
                int widthLeftSideCopyCheck = 45;

                int idx = 0;
                List<string> ls = new List<string>();

                e.Graphics.DrawString("Принято от: ", fontText1, Brushes.Black, leftMargin, topMarginNext, new StringFormat());

                KKM.SplitOnWidthString(ls, _cashRegister.AbonentName, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("Основание: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());
                e.Graphics.DrawString(_cashRegister.C_Foundation, fontText1, Brushes.Black, leftMargin+83, topMarginNext + heigthText1 * idx, new StringFormat());
                idx++;

                string rusCurrence = RusCurrency.Str(Convert.ToDouble(_cashRegister.Amount, CultureInfo.InvariantCulture));
                e.Graphics.DrawString("Сумма: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx , new StringFormat());

                ls.Clear();
                KKM.SplitOnWidthString(ls, rusCurrence, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("В том числе: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());
                PayNDS payNds = Tools.GetTax(_cashRegister.Amount, _cashRegister.N_Tax);
                string include = String.Format("{0} - {1}; {2} - {3}", _cashRegister.Sale_Items_Name, payNds.Value.ToString("C"), FieldsName.CashAccountTax, payNds.Tax.ToString("C"));
                ls.Clear();
                KKM.SplitOnWidthString(ls, include, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }
                e.Graphics.DrawString("Приложение: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());

                e.Graphics.DrawString("Получил кассир: ", fontText1B, Brushes.Black, leftMargin, e.MarginBounds.Top + 480, new StringFormat());
                e.Graphics.DrawString(_cashRegister.CreatedEmployer, fontText2, Brushes.Black, leftMargin + 265, e.MarginBounds.Top + 480, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin+140, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 230, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawLine(linelPen1, leftMargin + 260, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 385, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawString("подпись ", fontText3, Brushes.Black, leftMargin+160, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());
                e.Graphics.DrawString("расшифровка подписи ", fontText3, Brushes.Black, leftMargin + 280, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());

                #endregion

                // Вертикаль
                e.Graphics.DrawLine(linelPen2, leftMargin + 445, e.MarginBounds.Top, leftMargin + 445, e.MarginBounds.Top + 500);
                e.Graphics.DrawLine(linelPen2, leftMargin + 458, e.MarginBounds.Top, leftMargin + 458, e.MarginBounds.Top + 500);

                //Тело чека
                float startTopShift = 85;
                float startLeftShift = 17;

                e.Graphics.DrawString(CashEmployer.Inst.SvDeptName, fontText1B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1, leftMargin + 690, e.MarginBounds.Top + heigthText1);
                
                e.Graphics.DrawString("КОПИЯ ЧЕКА", fontText1, Brushes.Black, leftMargin + 530, e.MarginBounds.Top+40, new StringFormat());


                e.Graphics.DrawString(String.Format("№: {0}", _cashRegister.Number), fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 70, new StringFormat());
                e.Graphics.DrawString(String.Format("от: {0}", _cashRegister.DatePrint.ToShortDateString()), fontText1, Brushes.Black, leftMargin + 580 + startLeftShift, e.MarginBounds.Top + 70, new StringFormat());

                idx = 0;
                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Принято от:", _cashRegister.AbonentName), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Основание: ", _cashRegister.C_Foundation), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }
                
                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Сумма: ", rusCurrence), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "В том числе: ", include), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("М.П. (штампа) ", fontText1B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 400, new StringFormat());
                e.Graphics.DrawString("Кассир ", fontText2B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 430, new StringFormat());
                e.Graphics.DrawString(_cashRegister.CreatedEmployer, fontText2, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 450, new StringFormat());

                e.Graphics.DrawLine(linelPen1, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 620, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawString("расшифровка подписи ", fontText3, Brushes.Black, leftMargin + 480 + startLeftShift, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());

	        }
	        finally
	        {
                linelPen1.Dispose();
                linelPen2.Dispose();
	        }

        }

        #endregion

        private FormMain _formMain;
        private CashRegister _cashRegister;
        private Font fontText0 = new Font(FontFamily.GenericSansSerif, 11); private Font fontText0B = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold);
        private Font fontText1 = new Font(FontFamily.GenericSansSerif, 10); private Font fontText1B = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
        private Font fontText2 = new Font(FontFamily.GenericSansSerif, 8); private Font fontText2B = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold);
        private Font fontText3 = new Font(FontFamily.GenericSansSerif, 6); private Font fontText3B = new Font(FontFamily.GenericSansSerif, 6, FontStyle.Bold);


    }
    
    /// <summary>
    /// Печать не продажного чека
    /// </summary>
    public class PrintDoc02 : IPrintDoc
    {
        public void Init()
        {
            _formMain = Tools.MainForm as FormMain;
        }

        public void PrintCash(CashRegister cashRegister)
        {
            try
            {

                KKM kkm = KKM.Inst;
                FrkDrv.Inst.Password = 30;
                FrkDrv.Inst.CheckType = 0;

                Sale(cashRegister, kkm, cashRegister.Amount);
            }
            catch (Exception err)
            {
                throw new ECash(MessageType.AllertLog, DriverError.CheckPrintError, err);
            }
            finally
            {
                FrkDrv.Inst.CloseCheck();
                cashRegister.Number = FrkDrv.Inst.OpenDocumentNumber + Properties.Settings.Default.CheckNumberShift;
            }

        }
        private void Sale(CashRegister cashRegister, KKM kkm, double Amount)
        {
            FrkDrv.Inst.OpenCheck();

            FrkDrv.Inst.Price = Convert.ToDecimal(Amount, CultureInfo.InvariantCulture);
            FrkDrv.Inst.Quantity = 1;
            FrkDrv.Inst.Department = 1;
            FrkDrv.Inst.StringForPrinting = cashRegister.Sale_Items_Name;
            FrkDrv.Inst.Connect();
            FrkDrv.Inst.Sale();

            // Внесенная сумма
            FrkDrv.Inst.Summ1 = Convert.ToDecimal(cashRegister.Amount, CultureInfo.InvariantCulture);

            FrkDrv.Inst.UseReceiptRibbon = true;
            FrkDrv.Inst.UseJournalRibbon = false;

            CashPrintText("-----------------------------------");

            List<string> ls = new List<string>();
            KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Принято от:", cashRegister.AbonentName), kkm.WidthCheck);
            for (int i = 0; i < ls.Count; i++)
                CashPrintText(ls[i]);

            ls.Clear();
            KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Основание: ", cashRegister.C_Foundation), kkm.WidthCheck);
            for (int i = 0; i < ls.Count; i++)
                CashPrintText(ls[i]);

            CashPrintText("-----------------------------------");

        }
        private void CashPrintText(string str)
        {
            FrkDrv.Inst.StringForPrinting = str;
            FrkDrv.Inst.PrintString();
            FrkDrv.Inst.StringForPrinting = "";
        }
        public void PrintCopyCash(CashRegister cashRegister)
        {
            _cashRegister = cashRegister;
            _formMain.Print(new PrintPageEventHandler(DrawCashCopy));
        }
        public void PrinPreview(CashRegister cashRegister)
        {
            _formMain = Tools.MainForm as FormMain;
            _cashRegister = cashRegister;
            if (_formMain != null)
            {
                _formMain.PreviewDialogShow(new PrintPageEventHandler(DrawCashCopy));
            }
        }
        public void ReportWithoutCleaning()
        {
            FrkDrv.Inst.PrintReportWithoutCleaning();
        }
        public void ReportWithCleaning()
        {
            FrkDrv.Inst.PrintReportWithCleaning();
        }

        #region Печать копии чека

        private void DrawCashCopy(object sender, PrintPageEventArgs e)
        {
            Pen linelPen1 = new Pen(Color.Black, 1);
            Pen linelPen2 = new Pen(Color.Black, 2);
            KKM kkm = KKM.Inst;
            try
            {
                float topMargin = e.MarginBounds.Top + 60;
                float leftMargin = e.MarginBounds.Left;

                float heigthText1 = fontText1.GetHeight(e.Graphics) + 3;
                float heigthText3 = fontText3.GetHeight(e.Graphics) + 3;

                // Строка 1
                e.Graphics.DrawString(CashEmployer.Inst.SvDeptName, fontText1B, Brushes.Black, leftMargin, topMargin, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin, topMargin + heigthText1, leftMargin + 280, topMargin + heigthText1);

                #region Номер документа, дата составления

                e.Graphics.DrawString("КОПИЯ ЧЕКА", fontText1B, Brushes.Black, leftMargin + 80, topMargin + 60, new StringFormat());
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 140, 50);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 140, 50);
                e.Graphics.DrawRectangle(linelPen2, leftMargin + 280, topMargin + 67, 140, 23);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 280, topMargin + 40, 70, 50);
                e.Graphics.DrawString("Номер", fontText3, Brushes.Black, leftMargin + 300, topMargin + 42, new StringFormat());
                e.Graphics.DrawString("документа", fontText3, Brushes.Black, leftMargin + 292, topMargin + 51, new StringFormat());
                e.Graphics.DrawString("Дата", fontText3, Brushes.Black, leftMargin + 370, topMargin + 42, new StringFormat());
                e.Graphics.DrawString("составления", fontText3, Brushes.Black, leftMargin + 355, topMargin + 51, new StringFormat());
                e.Graphics.DrawString(_cashRegister.Number.ToString(), fontText2, Brushes.Black, leftMargin + 285, topMargin + 70, new StringFormat());
                e.Graphics.DrawString(_cashRegister.DatePrint.ToShortDateString(), fontText2, Brushes.Black, leftMargin + 350, topMargin + 70, new StringFormat());

                #endregion

                #region Таблица

                e.Graphics.DrawRectangle(linelPen2, leftMargin, topMargin + 100, 420, 100);
                e.Graphics.DrawRectangle(linelPen2, leftMargin, topMargin + 180, 420, 20);

                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 60, 100);
                e.Graphics.DrawString("Дебет", fontText2, Brushes.Black, leftMargin + 13, topMargin + 134, new StringFormat());
                e.Graphics.DrawString("Кредит", fontText2, Brushes.Black, leftMargin + 145, topMargin + 103, new StringFormat());

                e.Graphics.DrawString("код струк-", fontText3, Brushes.Black, leftMargin + 83, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("турного под-", fontText3, Brushes.Black, leftMargin + 77, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("разделения", fontText3, Brushes.Black, leftMargin + 79, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("кореспон-", fontText3, Brushes.Black, leftMargin + 145, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("дирующий", fontText3, Brushes.Black, leftMargin + 142, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("счет, субсч.", fontText3, Brushes.Black, leftMargin + 140, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("код аналити-", fontText3, Brushes.Black, leftMargin + 197, topMargin + 133, new StringFormat());
                e.Graphics.DrawString("ческого", fontText3, Brushes.Black, leftMargin + 206, topMargin + 141, new StringFormat());
                e.Graphics.DrawString("учета", fontText3, Brushes.Black, leftMargin + 210, topMargin + 149, new StringFormat());

                e.Graphics.DrawString("Сумма,", fontText2, Brushes.Black, leftMargin + 268, topMargin + 120, new StringFormat());
                e.Graphics.DrawString("руб. коп.", fontText2, Brushes.Black, leftMargin + 266, topMargin + 132, new StringFormat());

                e.Graphics.DrawString("Код", fontText2, Brushes.Black, leftMargin + 348, topMargin + 120, new StringFormat());
                e.Graphics.DrawString("целевого", fontText2, Brushes.Black, leftMargin + 336, topMargin + 130, new StringFormat());
                e.Graphics.DrawString("назначения", fontText2, Brushes.Black, leftMargin + 330, topMargin + 140, new StringFormat());

                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 75, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 75, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 135, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin + 195, topMargin + 120, 60, 80);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 255, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 325, 100);
                e.Graphics.DrawRectangle(linelPen1, leftMargin, topMargin + 100, 400, 100);

                e.Graphics.DrawString("50.1", fontText2, Brushes.Black, leftMargin + 3, topMargin + 182, new StringFormat());
                e.Graphics.DrawString("62.7", fontText2, Brushes.Black, leftMargin + 137, topMargin + 182, new StringFormat());
                e.Graphics.DrawString(_cashRegister.Amount.ToString("C"), fontText2, Brushes.Black, leftMargin + 257, topMargin + 182, new StringFormat());

                #endregion

                #region Текст

                float topMarginNext = topMargin + 220;
                int widthLeftSideCopyCheck = 45;

                int idx = 0;
                List<string> ls = new List<string>();

                e.Graphics.DrawString("Принято от: ", fontText1, Brushes.Black, leftMargin, topMarginNext, new StringFormat());

                KKM.SplitOnWidthString(ls, _cashRegister.AbonentName, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("Основание: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());
                e.Graphics.DrawString(_cashRegister.C_Foundation, fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                idx++;

                string rusCurrence = RusCurrency.Str(Convert.ToDouble(_cashRegister.Amount, CultureInfo.InvariantCulture));
                e.Graphics.DrawString("Сумма: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());

                ls.Clear();
                KKM.SplitOnWidthString(ls, rusCurrence, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("В том числе: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());
                string include = String.Format("{0} - {1}", _cashRegister.Sale_Items_Name, _cashRegister.Amount.ToString("C"));
                ls.Clear();
                KKM.SplitOnWidthString(ls, include, widthLeftSideCopyCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 83, topMarginNext + heigthText1 * idx, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("Приложение: ", fontText1, Brushes.Black, leftMargin, topMarginNext + heigthText1 * idx, new StringFormat());

                e.Graphics.DrawString("Получил кассир: ", fontText1B, Brushes.Black, leftMargin, e.MarginBounds.Top + 480, new StringFormat());
                e.Graphics.DrawString(_cashRegister.CreatedEmployer, fontText2, Brushes.Black, leftMargin + 265, e.MarginBounds.Top + 480, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin + 140, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 230, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawLine(linelPen1, leftMargin + 260, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 385, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawString("подпись ", fontText3, Brushes.Black, leftMargin + 160, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());
                e.Graphics.DrawString("расшифровка подписи ", fontText3, Brushes.Black, leftMargin + 280, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());

                #endregion

                // Вертикаль
                e.Graphics.DrawLine(linelPen2, leftMargin + 445, e.MarginBounds.Top, leftMargin + 445, e.MarginBounds.Top + 500);
                e.Graphics.DrawLine(linelPen2, leftMargin + 458, e.MarginBounds.Top, leftMargin + 458, e.MarginBounds.Top + 500);

                //Тело чека
                float startTopShift = 85;
                float startLeftShift = 17;

                e.Graphics.DrawString(CashEmployer.Inst.SvDeptName, fontText1B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top, new StringFormat());
                e.Graphics.DrawLine(linelPen1, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1, leftMargin + 690, e.MarginBounds.Top + heigthText1);

                e.Graphics.DrawString("КОПИЯ ЧЕКА", fontText1, Brushes.Black, leftMargin + 530, e.MarginBounds.Top + 40, new StringFormat());


                e.Graphics.DrawString(String.Format("№: {0}", _cashRegister.Number), fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 70, new StringFormat());
                e.Graphics.DrawString(String.Format("от: {0}", _cashRegister.DatePrint.ToShortDateString()), fontText1, Brushes.Black, leftMargin + 580 + startLeftShift, e.MarginBounds.Top + 70, new StringFormat());

                idx = 0;
                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Принято от:", _cashRegister.AbonentName), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Основание: ", _cashRegister.C_Foundation), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "Сумма: ", rusCurrence), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                ls.Clear();
                KKM.SplitOnWidthString(ls, String.Format("{0} {1}", "В том числе: ", include), kkm.WidthCheck);
                for (int i = 0; i < ls.Count; i++)
                {
                    e.Graphics.DrawString(ls[i], fontText1, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + heigthText1 * idx + startTopShift, new StringFormat());
                    idx++;
                }

                e.Graphics.DrawString("М.П. (штампа) ", fontText1B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 400, new StringFormat());
                e.Graphics.DrawString("Кассир ", fontText2B, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 430, new StringFormat());
                e.Graphics.DrawString(_cashRegister.CreatedEmployer, fontText2, Brushes.Black, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 450, new StringFormat());

                e.Graphics.DrawLine(linelPen1, leftMargin + 450 + startLeftShift, e.MarginBounds.Top + 480 + heigthText1, leftMargin + 620, e.MarginBounds.Top + 480 + heigthText1);
                e.Graphics.DrawString("расшифровка подписи ", fontText3, Brushes.Black, leftMargin + 480 + startLeftShift, e.MarginBounds.Top + 485 + heigthText3, new StringFormat());

            }
            finally
            {
                linelPen1.Dispose();
                linelPen2.Dispose();
            }

        }

        #endregion

        private FormMain _formMain;
        private CashRegister _cashRegister;
        private Font fontText0 = new Font(FontFamily.GenericSansSerif, 11); private Font fontText0B = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold);
        private Font fontText1 = new Font(FontFamily.GenericSansSerif, 10); private Font fontText1B = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
        private Font fontText2 = new Font(FontFamily.GenericSansSerif, 8); private Font fontText2B = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold);
        private Font fontText3 = new Font(FontFamily.GenericSansSerif, 6); private Font fontText3B = new Font(FontFamily.GenericSansSerif, 6, FontStyle.Bold);


    }
}


